package tv.huan.web.controller.tencent;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import tv.huan.common.annotation.Log;
import tv.huan.common.core.controller.BaseController;
import tv.huan.common.core.domain.AjaxResult;
import tv.huan.common.core.page.TableDataInfo;
import tv.huan.common.enums.BusinessType;
import tv.huan.common.utils.poi.ExcelUtil;
import tv.huan.framework.util.ShiroUtils;
import tv.huan.system.domain.SysUser;
import tv.huan.tencent.domain.CardPool;
import tv.huan.tencent.service.ICardPoolService;

/**
 * 卡池Controller
 * 
 * @author admin
 * @date 2020-12-23
 */
@Controller
@RequestMapping("/tencent/cardPool")
public class CardPoolController extends BaseController
{
    private String prefix = "tencent/cardPool";

    @Autowired
    private ICardPoolService cardPoolService;

    @RequiresPermissions("tencent:cardPool:view")
    @GetMapping()
    public String cardPool()
    {
        return prefix + "/cardPool";
    }

    /**
     * 查询卡池列表
     */
    @RequiresPermissions("tencent:cardPool:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CardPool cardPool)
    {
        startPage();
        List<CardPool> list = cardPoolService.selectCardPoolList(cardPool);
        return getDataTable(list);
    }

    /**
     * 导出卡池列表
     */
    @RequiresPermissions("tencent:cardPool:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CardPool cardPool)
    {
        List<CardPool> list = cardPoolService.selectCardPoolList(cardPool);
        ExcelUtil<CardPool> util = new ExcelUtil<CardPool>(CardPool.class);
        return util.exportExcel(list, "cardPool");
    }

    /**
     * 新增卡池
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存卡池
     */
    @RequiresPermissions("tencent:cardPool:add")
    @Log(title = "卡池", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CardPool cardPool)
    {
        if (null != cardPoolService.checkCardPool(cardPool)){
            return AjaxResult.error("卡池已存在！");
        }
        SysUser user = ShiroUtils.getSysUser();
        cardPool.setCreateBy(user.getUserName());
        return toAjax(cardPoolService.insertCardPool(cardPool));
    }

    /**
     * 修改卡池
     */
    @GetMapping("/edit/{cardId}")
    public String edit(@PathVariable("cardId") Integer cardId, ModelMap mmap)
    {
        CardPool cardPool = cardPoolService.selectCardPoolById(cardId);
        mmap.put("cardPool", cardPool);
        return prefix + "/edit";
    }

    /**
     * 修改保存卡池
     */
    @RequiresPermissions("tencent:cardPool:edit")
    @Log(title = "卡池", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CardPool cardPool)
    {
        return toAjax(cardPoolService.updateCardPool(cardPool));
    }

    /**
     * 删除卡池
     */
    @RequiresPermissions("tencent:cardPool:remove")
    @Log(title = "卡池", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(cardPoolService.deleteCardPoolByIds(ids));
    }
}
